package com.longcheer.weibo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.longcheer.weibo.util.WeiboConstants;
import com.longcheer.weibo.vo.Comment;
import com.longcheer.weibo.vo.Message;
import com.longcheer.weibo.vo.Province;
import com.longcheer.weibo.vo.Status;
import com.longcheer.weibo.vo.User;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;
import com.weibo.net.WeiboParameters;

public class WeiboHelper {

    private static WeiboHelper instance;
    
    private WeiboHelper() {
        
    }
    
    public static WeiboHelper getInstance() {
        if (instance == null) {
            instance = new WeiboHelper();
        }
        
        return instance;
    }
    
    public List<Status> getFriendTimeLine(Context context) throws WeiboException {
        String url = WeiboConstants.API_SERVER + "statuses/friends_timeline.json";
        WeiboParameters bundle = new WeiboParameters();
        Weibo weibo = Weibo.getInstance();
        bundle.add("source", Weibo.APP_KEY);
        String resp = weibo.request(context, url, bundle, "GET", weibo.getAccessToken());
        
        JSONArray statuses;
        try {
            statuses = new JSONArray(resp);
            List<Status> friendTimeline = new ArrayList<Status>();
            for (int i = 0; i < statuses.length(); ++i) {
                friendTimeline.add(new Status(statuses.getJSONObject(i)));
            }
            return friendTimeline;
        } catch (JSONException e) {
            throw new WeiboException(e.getMessage(), e);
        }
    }
    
    public List<Comment> getCommentListByWeiboId(Context context, long weiboId, int page) throws WeiboException {
        String url = WeiboConstants.API_SERVER + "statuses/comments.json";
        WeiboParameters bundle = new WeiboParameters();
        Weibo weibo = Weibo.getInstance();
        bundle.add("source", Weibo.APP_KEY);
        bundle.add("id", String.valueOf(weiboId));
        bundle.add("page", String.valueOf(page));
        String resp = weibo.request(context, url, bundle, "GET", weibo.getAccessToken());
        
        return getCommentList(resp);
    }
    
    public List<Comment> getMyCommentListReceive(Context context, int count) throws WeiboException {
    	return getMyCommentListReceive(context, count, -1, -1);
    }
    
    public List<Comment> getMyCommentListReceive(Context context, int count, long maxId, long sinceId) throws WeiboException {
        String url = WeiboConstants.API_SERVER + "statuses/comments_to_me.json";
        WeiboParameters bundle = new WeiboParameters();
        Weibo weibo = Weibo.getInstance();
        bundle.add("source", Weibo.APP_KEY);
        //bundle.add("count", String.valueOf(count));
        if (maxId > 0) {
        	bundle.add("max_id", String.valueOf(maxId));
        }
        if (sinceId > 0) {
        	bundle.add("since_id", String.valueOf(sinceId));
        }
        
        String resp = weibo.request(context, url, bundle, "GET", weibo.getAccessToken());
        
        return getCommentList(resp);
    }
    
    private List<Comment> getCommentList(String jsonString) throws WeiboException {
        List<Comment> comments = new ArrayList<Comment>();
        try {
            JSONArray commentArray = new JSONArray(jsonString);
            for (int i = 0; i < commentArray.length(); ++i) {
                Comment comment = new Comment(commentArray.getJSONObject(i));
                comments.add(comment);
            }
        } catch (JSONException e) {
            throw new WeiboException(e.getMessage(), e);
        }
        
        return comments;
    }
    
    public User getUserByUserId(Context context, String userId) throws WeiboException {
        String url = WeiboConstants.API_SERVER + "users/show.json";
        WeiboParameters bundle = new WeiboParameters();
        Weibo weibo = Weibo.getInstance();
        bundle.add("id", userId);
        String resp = weibo.request(context, url, bundle, "GET", weibo.getAccessToken());
        
        try {
            JSONObject json = new JSONObject(resp);
            return new User(json);
        } catch (JSONException e) {
            throw new WeiboException(e.getMessage(), e);
        }
    }
    
    public Map<Integer, Province> getProvinces(Context context) throws WeiboException {
        String url = WeiboConstants.API_SERVER + "provinces.json";
        WeiboParameters bundle = new WeiboParameters();
        Weibo weibo = Weibo.getInstance();
        String resp = weibo.request(context, url, bundle, "GET", weibo.getAccessToken());
        
        Map<Integer, Province> provinceMap = new HashMap<Integer, Province>();
        try {
            JSONObject json = new JSONObject(resp);
            JSONArray array = json.getJSONArray("provinces");
            for (int i = 0; i < array.length(); ++i) {
                Province province = new Province(array.getJSONObject(i));
                provinceMap.put(province.getCode(), province) ;
            }
            
            return provinceMap;
        } catch (JSONException e) {
            throw new WeiboException(e.getMessage(), e);
        }
    }
    
    public List<User> getFriendList(Context context, long userId) throws WeiboException {
        return getUserList(context, userId, false);
    }
    
    public List<User> getFollowList(Context context, long userId) throws WeiboException {
        return getUserList(context, userId, true);
    }
    
    public List<User> getUserList(Context context, long userId, boolean follow) throws WeiboException {
        String url = null;
        if (follow) {
            url = WeiboConstants.API_SERVER + "statuses/followers/" + userId + ".json";
        } else {
            url = WeiboConstants.API_SERVER + "statuses/friends/" + userId + ".json";
        }
        WeiboParameters bundle = new WeiboParameters();
        Weibo weibo = Weibo.getInstance();
        bundle.add("source", Weibo.APP_KEY);
        String resp = weibo.request(context, url, bundle, "GET", weibo.getAccessToken());
        
        List<User> userList = new ArrayList<User>();
        try {
            JSONArray array = new JSONArray(resp);
            for (int i = 0; i < array.length(); ++i) {
                userList.add(new User(array.getJSONObject(i)));
            }
            
            return userList;
        } catch (JSONException e) {
            throw new WeiboException(e.getMessage(), e);
        }
    }
    
    public User verifyUser(Context context) throws WeiboException {
        String url = WeiboConstants.API_SERVER + "account/verify_credentials.json";
        WeiboParameters bundle = new WeiboParameters();
        Weibo weibo = Weibo.getInstance();
        bundle.add("source", Weibo.APP_KEY);
        String resp = weibo.request(context, url, bundle, "GET", weibo.getAccessToken());
        
        try {
            JSONObject json = new JSONObject(resp);
            return new User(json);
        } catch (JSONException e) {
            throw new WeiboException(e.getMessage(), e);
        }
    }
}
